package com.ylfstu.generic;

import java.util.ArrayList;
import java.util.Comparator;

public class GenericExercise02 {
    public static void main(String[] args) {
        ArrayList<Employee> employees = new ArrayList<>();
        employees.add(new Employee("jack", 20000, new MyDate(12, 12, 1998)));
        employees.add(new Employee("tom", 12000, new MyDate(5, 12, 2000)));
        employees.add(new Employee("jack", 15000, new MyDate(12, 20, 1998)));
//        System.out.println(employees);
        for (Employee employee : employees) {
            System.out.println(employee);
        }
        System.out.println("-----排序-----");
        employees.sort(new Comparator<Employee>() {
            @Override
            public int compare(Employee o1, Employee o2) {
                if (o1 == null || o2 == null) {
                    System.out.println("不能为空...");
                    return 0;
                }
                //比较name
                int i = o1.getName().compareTo(o2.getName());
                if (i != 0)
                    return i;

                //比较year month day
                return o1.getBirthday().compareTo(o2.getBirthday());

            }
        });
        for (Employee employee : employees) {
            System.out.println(employee);
        }

    }
}

class Employee {
    private String name;
    private double sal;
    private MyDate birthday;

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", sal=" + sal +
                ", birthday=" + birthday +
                '}';
    }

    public Employee(String name, double sal, MyDate birthday) {
        this.name = name;
        this.sal = sal;
        this.birthday = birthday;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    }

    public MyDate getBirthday() {
        return birthday;
    }

    public void setBirthday(MyDate birthday) {
        this.birthday = birthday;
    }
}

class MyDate implements Comparable<MyDate> {
    private int month;
    private int day;
    private int year;

    @Override
    public int compareTo(MyDate o) {
        //如果name 相同，就比较 year
        int i1 = year - o.getYear();
        if (i1 != 0)
            return i1;

        //如果year相同，就比较 month
        int i2 = month - o.getMonth();
        if (i2 != 0)
            return i2;

        //如果 year 和 month 都相同 ，就比较 day
        return day - o.getDay();
    }

    @Override
    public String toString() {
        return "MyDate{" +
                "month=" + month +
                ", day=" + day +
                ", year=" + year +
                '}';
    }

    public MyDate(int month, int day, int year) {
        this.month = month;
        this.day = day;
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

}
